package com.hzx.controller;

import com.hzx.pojo.Commodity;
import com.hzx.pojo.Recommand;
import com.hzx.service.*;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {

    @Autowired
    private ItemService itemService ;

    @Autowired
    private AdvertService advertService ;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private OrderService orderService;

    //页面跳转
    @GetMapping({"/home","/home.html"})
    public String home(Model model){
//        List itemList = getHomeItems();
//        List pictureList = getHomeSlide();
//        model.addAttribute("items",itemList) ;
//        model.addAttribute("slides",pictureList) ;

        Integer commodityNum = commodityService.selectcommodityNum(); // 发布的总商品数 由 自增的 id 直接获取
        Integer userNum = userInformationService.selectUserNum();  // 用户数量 由自增的 id 直接获取
        Integer orderNum = orderService.selectOrderNum();   // 发布求购的数量 由自增id 直接获取
        Integer commodityTradeNum = commodityService.selectCommodityTradeNum(); // 商品发布 成交量,通过选择 status = 1 获取
        Integer orderTradeNum = orderService.selectOrderTradeNum();   // 求购发布 成交量,通过选择 status = 1 获取
        Integer tradeNum = commodityTradeNum + orderTradeNum;  // 总成交量

        model.addAttribute("commodityNum",commodityNum);
        model.addAttribute("userNum",userNum);
        model.addAttribute("orderNum",orderNum);
        model.addAttribute("tradeNum",tradeNum);
        return "home" ;
    }

    //获取放在首页的商品ID
    public List getHomeItems(){
        List uidList = itemService.getAllReCommand();
        List itemList = new ArrayList() ;
        for (Object o : uidList) {
            Recommand co = (Recommand) o ;
            Commodity commodity = itemService.getItemByPrimaryKey(co.getCid()) ;
            itemList.add(commodity) ;
         }
        return itemList ;
    }

    //获取广告图片
    public List getHomeSlide(){
        List advertList = advertService.getAdvertInhome() ;
        return advertList ;
    }

    //获取数据库中的一些实时数据
    public String getData(){

        return "" ;
    }

    //获取推荐的咨询
    public String getDiscrible(){
        return "" ;
    }

}